package com.candy.control;

import com.candy.bean.UserInfo;
import com.candy.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("/getUserInfoes")
    public String getUserInfoes(Model model){
       List<UserInfo> userinfoes= userInfoService.getUserInfoes();
        model.addAttribute("userinfoes",userinfoes);
        return "userinfomanager/showUserInfoes";

    }

    @RequestMapping("/addUserInfo")
    public String addUserInfo(UserInfo userInfo){
        userInfoService.addUserInfo(userInfo);
        return "forward:/getUserInfoes";
    }


    @RequestMapping("/showUserInfoById")
    public String showUserInfoById(@RequestParam("userid") int userId, Model model){
       UserInfo userInfo= userInfoService.getUserInfoById(userId);
        model.addAttribute("userinfo",userInfo);
        return "userinfomanager/updateUserInfo";
    }

    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(UserInfo userInfo){
        userInfoService.updateUserInfo(userInfo);
        return "forward:/getUserInfoes";
    }

    @RequestMapping("/deleteUserInfo")
    public String deleteUserInfo(@RequestParam("userid") int userId){
        userInfoService.deleteUserInfo(userId);
        return "forward:/getUserInfoes";
    }


}
